package com.situ.hospital.room.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.hospital.room.dao.IRoomDao;
import com.situ.hospital.room.model.RoomModel;

import util.IsEmpty;
import util.JDBCUtil;

public class RoomDaoImpl implements IRoomDao {
	@Override
	public Integer insert(RoomModel room) {
		String sql = "insert into room(room_id,room_address,department_name) values(?,?,?)";
		return JDBCUtil.dml(sql, room.getRoomId(),room.getRoomAddress(),room.getDepartmentName());
	}

	@Override
	public RoomModel selectModel(RoomModel room) throws SQLException {
		String sql = "select * from room where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, room, 2);
		List<RoomModel> backlist = JDBCUtil.dql(sql, list, RoomModel.class, room.mapGet());
		if (backlist == null || backlist.size() == 0) {
			return null;
		} else {
			return backlist.get(0);
		}
	}

	@Override
	public List<RoomModel> selectAll(RoomModel room) throws SQLException {
		String sql = "select * from room where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, room, 2);
		return JDBCUtil.dql(sql, list, RoomModel.class, room.mapGet());
	}

	@Override
	public Integer deleteById(RoomModel room) throws SQLException {
		String sql = "delete from room where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, room, 2);
		return JDBCUtil.dml(sql, list);
	}

	@Override
	public Integer updateById(RoomModel room) throws SQLException {
		String sql = "update room set ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, room, 3);
		return JDBCUtil.dml(sql, list);
	}
	@Override
	public Integer selectCount(RoomModel room) throws SQLException {
		String sql = "select count(1) from room where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, room, 2);
		return JDBCUtil.selectCount(sql, list);

	}

	/**
	 * @param sql
	 * @param list
	 * @param department
	 * @param n          1为insert 2为select 2为delete 3为update
	 * @return
	 */
	private String setSql(String sql, List<Object> list, RoomModel room, int m) {
		int n = 0;

		if (room.getId() != null && room.getId() == 1) {
			sql += "room_address like ? and department_name like ?";
			
			list.add(room.getRoomAddress());
			list.add(room.getDepartmentName());
			if (room.isPageOn()) {
				sql += " limit ?,?";
				list.add(room.getRowStart());
				list.add(room.getPageLimit());
			}
			return sql;
		}
		Integer id = room.getRoomId();
		if (!IsEmpty.isEmpty(id) && m != 3) {
			if (n == 1) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "room_id=?";
			list.add(id);
			n = m;
		}
		String address = room.getRoomAddress();
		if (!IsEmpty.isEmpty(address)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "room_address=?";
			list.add(address);
			n = m;
		}
		String departmentName = room.getDepartmentName();
		if (!IsEmpty.isEmpty(departmentName)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "department_name=?";
			list.add(departmentName);
			n = m;
		}
		if (m == 3) {
			sql += " where room_id=?";
			list.add(id);
		}

		return sql;
	}
}
